﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class HomeController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            //Session["IdPersonal"] = 1;//para ahorrarse el loguearse
            string strResponse = String.Empty;
            bool acceso = false;
            int IdPersonal = -1;
            //else
            //Msg.Text = "Login failed. Please check your user name and password and try again.";

            string personal = Convert.ToString(Request.Params.Get(0));
            string clave = Convert.ToString(Request.Params.Get(1));
            string eliminar = Convert.ToString(Request.Params.Get(2));
            try
            {
                //var validar = entidades.usuarios.Where(p => (p.usuario1 == personal && p.clave == clave));

                if (Membership.ValidateUser(personal, clave))
                {
                    var perfil = entidades.aspnet_Users.Single(u => u.UserName == personal);
                    //var member = entx.aspnet_Membership.Where(m => m.UserId == perfil.UserId && m.Password == clave);
                    IdPersonal = Convert.ToInt32(perfil.IdPersonal);
                    acceso = true;
                    //FormsAuthentication.RedirectFromLoginPage(UsernameTextbox.Text, NotPublicCheckBox.Checked);
                }

                if (eliminar == "0")
                {
                    Session["IdPersonal"] = 0;
                    //Response.AddHeader("hola","holas");
                    return View();
                    //return Content("OK");
                }
                if (acceso == true)
                {
                    //var usuariox = entidades.usuarios.First(p => (p.IdPersonal == IdPersonal));

                    //var proyecto = entidades.proyectoes.Join(entidades.usuarioproyectoes, p => p.idproyecto, up => up.idproyecto,
                    //   (p, up) => new { p.idproyecto, p.proyecto1, up.idusuario }).Where(up => up.idusuario == usuariox.idusuario)
                    //   .First();

                    //Session["idusuario"] = usuariox.idusuario;


                    Session["IdPersonal"] = IdPersonal;

                    var query = entidades.PADiferenciaPrestamos().First();
                    if (query.Mensaje.Equals(""))
                    {
                        return Content("OK");
                    }

                   else
                    {
                        strResponse = query.Mensaje;
                        return Content(strResponse);
                    }

                }
                return View();
            }
            catch (Exception e)
            {
                return Content(e.Message);
                //return View();
            }
        }
        public ActionResult Desktop()
        {
            int IdPersonal = Convert.ToInt32(Session["IdPersonal"].ToString());
            var query = entidades.PERSONALs.Where(p => p.IdPersonal == IdPersonal);

            var result = (from p in query.ToList()
                          join td in entidades.TIPODOCUMENTOIDENTIDADs on p.IdTipoDI equals td.IdTipoDI
                          join pa in entidades.PERSONALAPLICACIONs on p.IdPersonal equals pa.IdPersonal
                          join a in entidades.APLICACIONs on pa.IdAplicacion equals a.IdAplicacion
                          where a.IdPadreAplicacion == null && pa.Estado == true
                          select new
                          {
                              IdPersonal = p.IdPersonal,
                              Persona = p.ApePaterno.Trim() + " " + p.ApeMaterno.Trim() + " " + p.Nombres.Trim(),
                              Fotocheck = p.Fotocheck,
                              Tipodni = td.Descripcion,
                              Nrodi = p.NroDI,
                              NomAplicacion = a.NomAplicacion,
                              iconCls = a.iconCls,
                              module = a.module,
                              getModule = a.getModule,

                          }).ToList();
            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
